#!/usr/bin/env node

const fs = require('fs')
const path = require('path')
const inquirer = require('inquirer')
const ejs = require('ejs')

inquirer.prompt([
    {
        type: 'input',
        message: '页面名称',
        name: 'name',
        default: '我的页面',
    },
    {
        type: 'input',
        message: '版本号?',
        name: 'version',
        default: '1.0.0'
    }
]).then(answer => {
    console.log(answer)
    // 模板目录
    const tempDir = path.join(__dirname, "templates")

    // 目标目录
    const testDir = process.cwd()

    fs.readdir(tempDir, (err, files) => {
        if (err) {
            console.log(err)
        } else {
            console.log(files)

            files.forEach(file => {
                ejs.renderFile(path.join(tempDir, file), answer, (err, result) => {
                    if (err) {
                        console.log(err)
                    } else {
                        fs.writeFile(path.join(testDir, file), result, {}, (err, result) => {
                            if (err) {
                                console.log(err)
                            } else {
                                console.log('写入成功')
                            }
                        })
                    }
                })
            })
        }
    })

})